<?php
/**
 * @title 50. Pow(x, n) - 二分
 * @author start2004
 */

class Solution {

    /**
     * @param Float $x
     * @param Integer $n
     * @return Float
     */
    function myPow($x, $n) {
        /**
         * @since 2020-09-08 特殊情况
         */
        if($n == 0){
            return 1;
        } elseif($n == 1){
            return $x;
        } elseif($n < 0){
            /**
             * @since 2020-09-08 负数
             */
            return 1 / self::myPow($x, 0-$n);
        } else {
            if($n%2 == 1){
                return $x * self::myPow($x, $n-1);
            } else {
                return self::myPow($x * $x, $n>>1);
            }
        }
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.22_7.php
 */
$datas = [
    [2.00000, 10],
    [2.10000, 3],
    [2.00000, -2],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->myPow($data[0], $data[1]);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
